/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'isPc',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:2,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'title',
        title: '标题',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    // {
    //     id: 'agent.name',
    //     type: 'string',
    //     title: '代理商',
    //     headerClass: 'text-left width-100',
    //     columnClass: 'text-left width-100',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'lk',
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         if (record.agentId == 0){
    //             return '<span style="background:#ff7500;padding:2px 5px;color:white;">所有代理商</span>';
    //         }else{
    //             return record.agent.name;
    //         }
    //     }
    // },
    {
        id: 'linkUrl',
        title: '跳转链接',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: false,
    },
    {
        id: 'sort',
        title: '排序',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false
    },
    {
        id: 'isShow',
        title: '状态',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.showStatus.show,
            2:$lang.showStatus.hide
        }
    },
    {
        id: 'verify',
        title: '操作',
        type: 'string',
        'export':false,
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var editHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            var delHtml = "<a href='javascript:;' class='btn btn-danger btn-sm' onclick='OperateHandle.delBanner(" + record.id + ")' ><i class='fa fa fa-lg fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            var html = "";
            // 判断是否有编辑权限
            if ($("#bannerEdit") && $("#bannerEdit").val() == 1) {
                html += editHtml;
            }
            // 判断是否有删除权限
            if ($("#bannerDel") && $("#bannerDel").val() == 1) {
                html += delHtml;
            }
            return html;
        }
    },
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/banner/json/list',
    // exportURL: ncGlobal.adminRoot + 'banner/json/export',
    exportFileName: 'banner列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","sort","isShow"],Timestamp:["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['eq_isPc'] = 2;
//排序
grid.sortParameter.columnId = ['asc_id'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();

            $("#addForm").find("[name='title']").val("");
            $("#addForm").find("[name='picUrl']").val("");
            $("#addForm").find("[name='linkUrl']").val("");
            $("#addForm").find("[name='sort']").val("");
            // $("#addForm").find("[name='type']").val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot+"default_image.gif");

            $(".isShow").bootstrapSwitch('state', true);
            _refreshImage();


        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".parsley-type").remove();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#id").val(gridData.id);
            $("#title").val(gridData.title);
            $("#linkUrl").val(gridData.linkUrl);
            $("#sort").val(gridData.sort);
            if(gridData.isShow==1){
                $(".isShow").bootstrapSwitch('state', true);
            }else{
                $(".isShow").bootstrapSwitch('state', false);
            }

            // var obj = document.getElementById('agentEdit');
            // for(var i = 0; i < obj.options.length; i++){
            //     var tmp = obj.options[i].value;
            //     if(tmp == gridData.agentId){
            //         obj.options[i].selected = 'selected';
            //         break;
            //     }
            // }
            $('#editFormPicPath').val(gridData.picUrl);
            $('#editFormPicImg').attr('src', ncGlobal.fileRoot + gridData.picUrl);
            _refreshImage();
        });
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['eq_isPc'] = 2;
            grid.fastQueryParameters['lk_title'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });

        /**
         * 刷新addmodal 上的图片
         */
        function _refreshImage() {
            $(".viewImage").jqthumb({
                width: 180,
                height: 100,
                after: function (imgObj) {
                    imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
                }
            });
        }

        //图片上传
        $("#addFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }

                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicPath').val(data.result.data);
                    $('#addFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        /**
         * 上传插件绑定
         */
        $("#editFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicPath').val(data.result.data);
                    $('#editFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-编辑
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
    }

    /**
     * 删除入驻申请
     */
    function delBanner(id) {
        var tpl = '您确定要删除该banner吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "banner/json/del",
            data: {
                id: id
            },
            content: tpl
        });
    }



    //外部可调用
    return {
        bindEvent: _bindEvent,
        delBanner: delBanner,
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});